<?php
/* SVN FILE: $Id: add.php 224 2009-09-17 19:25:18Z supersmashbrothers $ */
/**
* ibMarket 2
* IP.Board 3.0
* Add category section
*
* @author $Author: supersmashbrothers $
* @copyright $Copyright$
* @version $Revision: 224 $
* @lastrevision $Date: 2009-09-17 19:25:18 +0000 (Thu, 17 Sep 2009) $
* @modifiedby $LastChangedBy: supersmashbrothers $
* @lastmodified $LastChangedDate: 2009-09-17 19:25:18 +0000 (Thu, 17 Sep 2009) $
* @filesource $URL: http://ipb-ibmarket.googlecode.com/svn/ibmarket2/trunk/admin/applications_addon/other/ibmarket/modules_admin/categories/add.php $
*/


if ( ! defined( 'IN_ACP' ) )
{
	print "<h1>Incorrect access</h1>You cannot access this file directly. If you have recently upgraded, make sure you upgraded 'admin.php'.";
	exit();
}

class admin_ibmarket_categories_add extends ipsCommand
{
	/**
	 * Skin object
	 *
	 * @access	private
	 * @var		object			Skin templates
	 */
	private $html;
	
	/**
	 * Main class entry point
	 *
	 * @access	public
	 * @param	object		ipsRegistry reference
	 * @return	void		[Outputs to screen]
	 */
	public function doExecute( ipsRegistry $registry )
	{
		$this->registry->getClass('class_permissions')->checkPermissionAutoMsg( 'category_manage' );
		
		$this->html = $this->registry->output->loadTemplate('cp_skin_categories');
		
		$this->registry->getClass('class_localization')->loadLanguageFile( array( 'admin_market', 'admin_categories' ) );
		
		if($this->request['request_method'] == 'get')
		{
			$category = array();
			$category['cat_id'] = 0;
			$category['cat_parent_id'] = -1;
			$category['cat_name'] = "Category Name";
			$category['cat_description'] = "Category Description";
			$category['cat_tax'] = "Category Name";
			$category['price_multiplier'] = 1;
			$category['cat_tax'] = 0;
			$this->registry->output->html = $this->html->categoryForm( $category, 'add', $this->lang->words['add_category'], $this->lang->words['add_category_button'] );
			
			$this->registry->getClass('output')->html_main .= $this->registry->getClass('output')->global_template->global_frame_wrapper();
			$this->registry->getClass('output')->sendOutput();
		}
		else
		{
			ipsRegistry::getClass('adminFunctions')->checkSecurityKey();
			
			$category = array();
			$category['cat_parent_id'] = is_array($this->caches['market_category'][$this->request['cat_parent_id']]) ? $this->request['cat_parent_id'] : -1;
			$category['cat_icon'] = $this->request['cat_icon'];
			$category['cat_name'] = $this->request['cat_name'];
			$category['cat_description'] = IPSText::getTextClass('bbcode')->preDbParse(IPSText::getTextClass('editor')->processRawPost('cat_description'));
			$category['price_multiplier'] = numbers::parseFloat($this->request['price_multiplier']);
			$category['price_multiplier'] = numbers::compare($category['price_multiplier']) == 0 ? 1 : $category['price_multiplier'];
			$category['cat_tax'] = numbers::parseFloat($this->request['cat_tax']);
			$category['cat_tax'] = numbers::min(100, numbers::max(0, $category['cat_tax']));
			$category['cat_order'] = count(marketRegistry::getCategoryTree($category['cat_parent_id']));
			$category['seo_name'] = IPSText::makeSeoTitle($category['cat_name']);
			
			$this->DB->insert('market_categories', $category);
			
			$this->cache->rebuildCache('market_category','ibmarket');
			
			ipsRegistry::getClass('adminFunctions')->saveAdminLog( sprintf( $this->lang->words['category_add_log'], $category['cat_name'] ) );
			
			$this->registry->output->global_message = $this->lang->words['category_added'];
			$this->registry->output->silentRedirectWithMessage( $this->settings['base_url']."module=categories&amp;section=manage&amp;category=".$category['cat_parent_id'] );
		}
	}
}